<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
            <el-form-item label="属性归属" prop="placeholderName">
                <el-input
                        v-model="queryParams.placeholderName"
                        placeholder="请输入属性归属名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="placeholder_templateList" size="mini" @selection-change="handleSelectionChange">
            <el-table-column type="index" align="center" />
            <el-table-column label="属性归属" align="center" prop="placeholderBelong" />
            <el-table-column label="签署方" align="center" prop="belongPartType">
                <template slot-scope="scope">
                    <span>{{ belongPartTypeOption.filter(item => scope.row.belongPartType === item.value)[0].label }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改属性模板对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="属性归属" prop="placeholderBelong">
                    <el-input v-model="form.placeholderBelong" placeholder="请输入属性归属" />
                </el-form-item>
                <el-form-item label="签署方" prop="belongPartType">
                    <el-select v-model="form.belongPartType" placeholder="请选择签署方">
                        <el-option v-for="item in belongPartTypeOption"
                                   :key="item.value"
                                   :label="item.label"
                                   :value="item.value"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="属性详情" prop="placeholderTemplates">
                    <el-button type="primary" @click="addPlaceholderTemplate">添加</el-button>
                </el-form-item>
                <el-row v-if="form.placeholderTemplates.length !== 0" style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3); padding: 15px 0 15px 20px; margin: 15px">
                    <el-row v-for="(item, index) in form.placeholderTemplates" :key="index" style="margin: 10px">
                        属性：<el-input v-model="item.placeholderName" style="width: 210px"/>
                        必填：<el-select v-model="item.require" style="width: 90px">
                                <el-option v-for="item in requireOption" :key="index" :label="item.label" :value="item.value"></el-option>
                            </el-select>
                        类型：<el-select v-model="item.placeholderTag" style="width: 130px">
                        <el-option v-for="item in placeholderTagOption" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                        <el-button icon="el-icon-minus" circle @click="removePlaceholderTemplate(index)" size="mini" style="margin-left: 10px"/>
                    </el-row>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listPlaceholder_template, getPlaceholder_template, delPlaceholder_template, addPlaceholder_template, updatePlaceholder_template, exportPlaceholder_template } from '@/api/zfw/contract/placeholder_template'

export default {
  name: 'Placeholder_template',
  components: {
  },
  data () {
    return {
      // 遮罩层
      loading: true,
      typeOption: [
        {
          label: '属性',
          value: 1
        },
        {
          label: '归属',
          value: 2
        }
      ],
      belongPartTypeOption: [
        {
          label: '非签署方',
          value: 0
        },
        {
          label: '企业',
          value: 1
        },
        {
          label: '个人',
          value: 2
        }
      ],
      placeholderTagOption: [
        {
          label: '文本',
          value: 0
        },
        {
          label: '身份证',
          value: 1
        },
        {
          label: '日期',
          value: 2
        },
        {
          label: '手机号',
          value: 3
        },
        {
          label: '数字',
          value: 4
        },
        {
          label: '签章',
          value: 5
        },
        {
          label: '借款期数',
          value: 6
        },
        {
          label: '月利率',
          value: 7
        },
        {
          label: '还款方式',
          value: 8
        },
        {
          label: '每月还款日',
          value: 9
        },
        {
          label: '合同金额',
          value: 10
        }
      ],
      requireOption: [
        {
          label: '否',
          value: 0
        },
        {
          label: '是',
          value: 1
        }
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 属性模板表格数据
      placeholder_templateList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        placeholderName: null,
        require: null
      },
      // 表单参数
      form: {
        placeholderBelong: null,
        belongPartType: 0,
        placeholderTemplates: []
      },
      // 表单校验
      rules: {
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询属性模板列表 */
    getList () {
      this.loading = true
      listPlaceholder_template(this.queryParams).then(response => {
        this.placeholder_templateList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        placeholderBelong: null,
        belongPartType: 0,
        placeholderTemplates: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加属性模板'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      getPlaceholder_template(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改属性模板'
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addPlaceholder_template(this.form).then(response => {
            this.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除属性归属为"' + row.placeholderBelong + '"的数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delPlaceholder_template(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有属性模板数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportPlaceholder_template(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    addPlaceholderTemplate () {
      this.form.placeholderTemplates.push({
        placeholderName: null,
        placeholderTag: 0,
        require: 0
      })
    },
    removePlaceholderTemplate (index) {
      this.form.placeholderTemplates.splice(index, 1)
    }
  }
}
</script>
